package com.dykj.fireFighting.server.socialUnit.dutymanger.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dykj.fireFighting.api.socialUnit.dutymanger.dto.DutyRecordDto;
import com.dykj.fireFighting.api.socialUnit.dutymanger.entity.DutyRecord;
import com.dykj.fireFighting.api.socialUnit.dutymanger.service.IDutyRecordService;
import com.dykj.fireFighting.api.socialUnit.dutymanger.vo.DutyRecordVo;
import com.dykj.fireFighting.common.core.service.impl.BaseServiceImpl;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.server.socialUnit.dutymanger.mapper.DutyRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

/**
 * 值班记录表管理
 *
 * @Author zly
 * @Date 2021-07-20 16:48:48
 */
@Slf4j
@DubboService
@Transactional(rollbackFor = Exception.class)
public class DutyRecordServiceImpl extends BaseServiceImpl<DutyRecordMapper, DutyRecord> implements IDutyRecordService {


    @Override
    public PageVo<DutyRecord> queryList(DutyRecordDto dutyRecordDto) {

        HashMap<String, Object> params = new HashMap<>(16);
        params.put("unitId", dutyRecordDto.getUnitId());
        if(null !=dutyRecordDto.getDutyUser()){
            params.put("dutyUser",dutyRecordDto.getDutyUser());
        }
        if(null !=dutyRecordDto.getDutyTel()){
            params.put("dutyTel",dutyRecordDto.getDutyTel());
        }
        if(null !=dutyRecordDto.getDutyStatus()){
            params.put("dutyStatus",dutyRecordDto.getDutyStatus());
        }
        if(null !=dutyRecordDto.getStartTime()){
            params.put("startTime",dutyRecordDto.getStartTime());
        }
        if(null !=dutyRecordDto.getEndTime()){
            params.put("endTime",dutyRecordDto.getEndTime());
        }

        Page page = new Page();
        // 设置当前页码
        page.setCurrent(dutyRecordDto.getPage());
        // 设置页大小
        page.setSize(dutyRecordDto.getLimit());

        return new PageVo<DutyRecord>(this.baseMapper.queryList(page, params));

    }

    @Override
    public DutyRecordVo getInfo(String code) {
        return this.baseMapper.getInfo(code);
    }


    @Override
    public int selectCount(String userId, String shiftId, String date) {
        return baseMapper.selectCount(userId, shiftId, date);
    }

    @Override
    public int getSocScore(String start, String end, String unitId) {
        int total = 10;
        int fen = baseMapper.getDeductionScore(start, end, unitId);
        int result = total - fen;
        if (result < 0) {
            result = 0;
        }
        return result;
    }
}
